package moodish.dummy;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import moodish.comm.ClientSideMessage.Type;

/**
 * Thread class for server comunication class. For each connection made,
 * there is a DealClient who will treat the received messages.
 * A DealClient contains an ObjectOutputStream, an ObjectInputStream, a Socket, a ServerCommDummy
 * and a nickname(String).
 * 
 * @author group27
 * @version 1
 */


public class DealWithClientMessages extends Thread{
	
	private ObjectOutputStream out;
	private ObjectInputStream in;
	private Socket socket;
	private ServerCommDummy servercomm;
	private String nick;
	
	/**
	 * Constructor.
	 * @param Socket socket of connection estabilished.
	 * @param ServerComm comunication server class.
	 * 
	 * @throws IOException            Thrown if a connection cannot be established.
	 * @throws ClassNotFoundException            Thrown if the Class cannot be found.
	 */
	
	public DealWithClientMessages(Socket socket, ServerCommDummy servercomm){
		this.socket=socket;
		this.servercomm = servercomm;
		try {
			in = new ObjectInputStream(socket.getInputStream());
			out = new ObjectOutputStream(socket.getOutputStream());
			nick = (String) in.readObject();
			
		} catch (IOException e) {
			
		} catch (ClassNotFoundException e) {
		 
	}
	}
	
	/**
	 * Receives messages from client and sends them to ServerComm.
	 * 
	 * @throws ClassNotFoundException   Thrown if the Class cannot be found.
	 * @throws IOException            Thrown if a connection cannot be established.
	 */
	
	public void run(){
		ServerSidesMessage message;
		while(true){
			message = null;
			try {
				message = (ServerSidesMessage) in.readObject();
				servercomm.addMessage(message);
			} catch (ClassNotFoundException e) {
				
			} catch (IOException e) {
				
			}
		}
	}
	
	/**
	 * Inform a client that another client is now a friend.
	 * @param newFriendship nickname of the new friend
	 * @throws IOException            Thrown if a connection cannot be established.
	 */
		
		public void sendNewFriendship(String newFriendship) {
			ClienteSideMessage friendship = new ClienteSideMessage(ClienteSideMessage.SERVER, newFriendship, Type.FRIENDSHIP);
			try {
				out.writeObject(friendship);
			} catch (IOException e) {
				
			}
			
		}

		/**
		 * Report an error to a client.
		 * 
		 * @param error description of the error.
		 * @throws IOException            Thrown if a connection cannot be established.
		 */

		public void sendError(String error) {
			ClienteSideMessage errormessage = new ClienteSideMessage(ClienteSideMessage.SERVER, error, Type.ERROR);
			try {
				out.writeObject(errormessage);
			} catch (IOException e) {
				
			}
			
		}
		
		/**
		 * Inform a client that a new client has connected.
		 * 
		 * @param userConnected nickname of the connected client.
		 * @throws IOException            Thrown if a connection cannot be established.
		 */
	
		public void sendClientConnected(String userConnected) {
			ClienteSideMessage connected = new ClienteSideMessage(ClienteSideMessage.SERVER, userConnected, Type.CONNECTED);
			try {
				out.writeObject(connected);
			} catch (IOException e) {
				
			}
			
		}

		/**
		 * Inform a client that another client disconnected. 
		 * 
		 * @param userDisconnected nickname of client who disconnected.
		 * @throws IOException            Thrown if a connection cannot be established.
		 */
		
		public void sendClientDisconnected(String userDisconnected) {
			ClienteSideMessage disconnected = new ClienteSideMessage(ClienteSideMessage.SERVER, userDisconnected, Type.DISCONNECTED);
			try {
				out.writeObject(disconnected);
			} catch (IOException e) {
				
			}
			
		}
		
		/**
		    * Send a Moodish message to a client. 
		    * 
		    * @param fromNickname The nickname of the client from which the message was sent
		    * @param message      The message 
		    * @throws IOException            Thrown if a connection cannot be established.
		    */
		
		public void sendMoodishMessage(String fromNicename,String message) {
			ClienteSideMessage moodishmessage = new ClienteSideMessage(fromNicename, message, Type.MOODISH_MESSAGE);
			try {
				out.writeObject(moodishmessage);
			} catch (IOException e) {
				
			}
			
		}

		/**
		 * Inform a client that a former friend has decided to not be friend anymore.
		 * 
		 * @param unfriendship nickname of the unfriendship
		 * @throws IOException            Thrown if a connection cannot be established.
		 */
		
		public void sendNewUnfriendship(String unFriendship) {
			ClienteSideMessage unfriendship = new ClienteSideMessage(ClienteSideMessage.SERVER, unFriendship, Type.UNFRIENDSHIP);
			try {
				out.writeObject(unfriendship);
			} catch (IOException e) {
				
			}
			
		}
		
		/**
		 * Disconnect client.
		 * 
		 *@throws IOException            Thrown if a connection cannot be established. 
		 */
		
		public void disconect(){
			try {
				socket.close();
			} catch (IOException e) {
				
			}
		}
		
		/**
		 * Checks if client is currently connected.
		 * 
		 * @return <b>true</b> if client is currently connected, otherwise <b>false</b>.
		 */
		
		public boolean isConnected(){
			return !(socket.isClosed());
		}
		

		/**
		 * Gets nickname of client.
		 * 
		 * @return nickname of client.
		 */
		
		public Object getNickname() {
			return nick;
		}
		
		
		

	
	
	

}
